<template>
    <div class="addadmin" style="width: 1280px;">
        <div class="admin_header">
            <div class="administrator_header">
                <div class="ax_default box_2" id="u9509">
                    <div id="u9509_div"></div>
                </div>
                <span class="header_words" v-show="type">新增职称</span>
                <span class="header_words" v-show="!type">更新职称</span>
            </div>

        </div>
        <div class="add_form">
            <div class="form_header">
                <span class="form_admin" v-show="type">编辑角色信息</span>
                <span class="form_admin" v-show="!type">更新角色信息</span>
            </div>
            <div class="form_news">
                <el-form :model="ruleForm" :rules="rules" label-position="left" ref="ruleForm" label-width="100px"
                    class="demo-ruleForm">
                    <el-form-item label="医生姓名" prop="userName">
                        <el-input v-model="ruleForm.userName" placeholder="请输入内容"></el-input>
                    </el-form-item>
                    <el-form-item label="职称名称" prop="name">
                        <el-input v-model="ruleForm.name" placeholder="请输入内容"></el-input>
                    </el-form-item>
                    <el-form-item label="挂号费" prop="cost">
                        <el-input v-model="ruleForm.cost" placeholder="请输入内容"></el-input>
                    </el-form-item>

                    <el-form-item label="职称描述" prop="proDescribe">
                        <el-input v-model="ruleForm.proDescribe" placeholder="请输入内容"></el-input>
                    </el-form-item>
                    <el-form-item label="是否启用" prop="delivery">
                        <el-switch v-model="ruleForm.setMealState"></el-switch>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm()">保存</el-button>
                        <el-button type="primary" plain @click="resetForm('ruleForm')">取消</el-button>
                    </el-form-item>
                </el-form>

            </div>

        </div>

    </div>
</template>
<script>
import { getUserList, addUser, updataUserData } from '@/api/titleManagement'

import createDate from '@/utils/createDate'

import axios from 'axios';
import { nanoid } from 'nanoid';

export default {
    name: 'Addtitle',
    data() {
        return {
            ruleForm: {
                userName: '',
                name: '',
                cost: '',
                setMealState: false,
                proDescribe: '',
                addDate: ''
            },
            rules: {
                userName: [
                    { required: true, message: '请输入职称名称', trigger: 'blur' },
                    { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '请输入职称名称', trigger: 'blur' },
                    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
                ],
                cost: [
                    { required: true, message: '请输入费用', trigger: 'blur' }
                ],
                proDescribe: [
                    { required: true, message: '请输入内容', trigger: 'blur' }
                ],
            },
            type: true
        }

    },
    methods: {
        // 判断是添加用户 还是更新用户 方法  
        isAdd() {
            if (this.$route.params.type == 'updata') {
                this.type = false
                let __userObj = this.$route.params._userObj
                this.ruleForm.userName = __userObj.userName
                this.ruleForm.name = __userObj.name
                this.ruleForm.cost = __userObj.cost
                this.ruleForm.setMealState = __userObj.setMealState
                this.ruleForm.proDescribe = __userObj.proDescribe

            } else {
                this.type = true
            }
        },

        async submitForm() {
            // 判断是添加用户 还是更新用户
            if (this.type) {
                // 自定义方法 生成时间 yyyy-MM-dd  hh:mm:ss
                if (
                    this.ruleForm.userName.trim() != '' &&
                    this.ruleForm.name.trim() != '' &&
                    this.ruleForm.cost.trim() != '' &&
                    this.ruleForm.proDescribe.trim() != ''

                ) {
                    let time = new Date()
                    let nowTime = createDate(time.toLocaleString('en-US', { hour12: false }).split(" "))

                    let _obj = {
                        id: nanoid(),
                        userName: this.ruleForm.userName,
                        name: this.ruleForm.name,
                        cost: this.ruleForm.cost,
                        proDescribe: this.ruleForm.proDescribe,
                        setMealState: this.ruleForm.setMealState,
                        addDate: nowTime,
                        isChecked: false,
                        isdelete: 0
                    }
                    // 添加用户
                    await addUser(_obj)
                    this.$message({
                        message: '添加成功',
                        type: 'success'
                    })
                    this.$router.push('/titlemanag')
                } else {
                    this.$message({
                        message: '请不要输入空内容',
                        type: 'error'
                    })
                }

            } else {
                // console.log('更新');
                // console.log(this.ruleForm);

                if (this.ruleForm.userName.trim() != '' &&
                    this.ruleForm.name.trim() != '' &&
                    this.ruleForm.cost.trim() != '' &&
                    this.ruleForm.proDescribe.trim() != '') {
                    let time = new Date()
                    let nowTime = createDate(time.toLocaleString('en-US', { hour12: false }).split(" "))
                    this.ruleForm.addDate = nowTime
                    await updataUserData(this.$route.params._userObj.id, this.ruleForm)
                    this.$message({
                        message: '更新成功',
                        type: 'success'
                    })
                    this.$router.push('/titlemanag')
                }else{
                    this.$message({
                        message: '请不要输入空内容',
                        type: 'error'
                    })
                }

            }
        },
        // 取消按钮
        resetForm(formName) {
            // if (this.type) {
            //     this.ruleForm = {
            //         name: '',
            //         cost: '',
            //         delivery: false,
            //         proDescribe: '',
            //     } 
            //      this.$router.push('/titlemanag')
            // } else {
            //     this.$router.push('/titlemanag')
            // }

             this.$router.push('/titlemanag')

        }
    },
    mounted() {
        // 判断是添加用户 还是更新用户
        this.isAdd()

    },
}
</script>
<style lang="scss" scoped>
.addadmin {
    margin: 0 auto;
}

.admin_header {
    display: flex;
    justify-content: space-between;
}

.administrator_header {
    display: flex;
    align-items: center;
}

#u9509 {
    border-width: 0px;
    width: 24px;
    height: 8px;
    display: flex;
    font-size: 16px;
    color: #006EFF;

}

#u9509_div {
    border-width: 0px;
    width: 24px;
    height: 8px;
    background: inherit;
    background-color: rgba(0, 110, 255, 1);
    border: none;
    border-radius: 4px;
    -moz-box-shadow: none;
    -webkit-box-shadow: none;
    box-shadow: none;
    font-size: 16px;
    color: #006EFF;
}

.header_words {
    margin-left: 20px;
    font-size: 24px;
}

.add_form {
    box-shadow: 0 0 20px #ccc;
    border-radius: 10px;
    margin-top: 20px;
    background-color: #fff;
}

.demo-ruleForm {
    width: 420px;
}

.form_header {
    border-radius: 10px 10px 0 0;
    height: 50px;
    line-height: 50px;
    font-size: 16px;
    background-color: rgba(242, 242, 242, 1);
    padding-left: 20px;
}

.form_admin {
    display: inline-block;
    color: #006EFF;
}

.form_news {
    padding: 20px 50px;
}

// .el-form-item__label{
//     text-align: left;
// }
.el-switch__core {
    background: #fff;
}

.el-button {
    padding: 12px 50px;
    font-size: 14px;
}
</style>